package com.yanxu.client;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025-05-29-2025
 * @Description: 处理客户端用户的 键盘输入
 */
public class UserInputHandler implements Runnable {

    private ChatClient chatClient;

    public UserInputHandler(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @Override
    public void run() {
        BufferedReader consoleReader = null;
        try {
            // 获取键盘输入 reader
            consoleReader = new BufferedReader(new InputStreamReader(System.in));

            // 读取键盘的输入
            String input;
            while ((input = consoleReader.readLine()) != null) {
                chatClient.sendMessage(input + "\n");

                // 客户端要退出了
                if ("bye".equals(input)) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (consoleReader != null) {
                chatClient.close(consoleReader);
            }
        }
    }
}